package controller

import (
	"github.com/gin-gonic/gin"
	"kf/models/dao"
	"kf/utils/R"
)

type ConfigController struct {
	ConfigDao *dao.ConfigDao
}

func (settingController *ConfigController) GetConfigs(c *gin.Context) {
	configs := settingController.ConfigDao.FindConfigs()
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": configs,
	})
}
func (settingController *ConfigController) GetConfig(c *gin.Context) {
	key := c.Query("key")
	config := settingController.ConfigDao.FindConfig(key)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": config,
	})
}

// 修改对应的config
type KeyValue struct {
	Key   *string `form:"key"`
	Value *string `form:"value"`
}

func (settingController *ConfigController) PostConfig(c *gin.Context) {
	var kv KeyValue
	c.ShouldBindJSON(&kv)
	if *kv.Key == "" || *kv.Value == "" {
		c.JSON(200, R.ReturnFailMsg("缺少参数传递"))
		return
	}
	settingController.ConfigDao.UpdateConfig(*kv.Key, *kv.Value)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
	})
}
